package utils

import (
	"cachecron/global"
	"github.com/go-redis/redis/v7"
)

var Client *redis.Client

func ExampleNewClient() *redis.Client {
	client := redis.NewClient(&redis.Options{
		Addr:     global.App.Configuration.Redis.Addr + ":" + global.App.Configuration.Redis.Port,
		Password: global.App.Configuration.Redis.Password, // no password set
		DB:       global.App.Configuration.Redis.DB,       // use default DB
	})

	_, err := client.Ping().Result()
	if err != nil {
		panic(err)
	}
	Client = client

	return client
}

func GetKeys() ([]string, error) {
	result, err := Client.Keys("*").Result()
	if err != nil {
		return nil, err
	} else {
		return result, nil
	}
}

func GetHash(key string) (map[string]string, error) {
	result, err := Client.HGetAll(key).Result()

	if err != nil {
		return nil, err
	} else {

		return result, nil
	}
}
